<html>
<head><meta charset="utf-8"><title>drop_in_place polymorphization · t-compiler/wg-polymorphization · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/216091-t-compiler/wg-polymorphization/index.html">t-compiler/wg-polymorphization</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/216091-t-compiler/wg-polymorphization/topic/drop_in_place.20polymorphization.html">drop_in_place polymorphization</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="206514913"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216091-t-compiler/wg-polymorphization/topic/drop_in_place%20polymorphization/near/206514913" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/216091-t-compiler/wg-polymorphization/topic/drop_in_place.20polymorphization.html#206514913">(Aug 10 2020 at 21:21)</a>:</h4>
<p>I'm not sure what exactly I need to do -- maybe I'm not testing right, but I'm wondering if we can avoid codegen'ing empty stubs via polymorphization (or something similar) for this case:</p>
<div class="codehilite"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">usize_</span><span class="p">(</span><span class="n">v</span>: <span class="kp">&amp;</span><span class="kt">usize</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="nc">dyn</span><span class="w"> </span><span class="nb">Send</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">v</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">u32_</span><span class="p">(</span><span class="n">v</span>: <span class="kp">&amp;</span><span class="kt">u32</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="nc">dyn</span><span class="w"> </span><span class="nb">Send</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">v</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>Currently this produces the following LLVM IR, without optimizations turned on (which is AFAIK basically what we give LLVM to chew on). While it isn't horrible, I would love to avoid codegen'ing two separate drop_in_place's -- they are clearly identical, indeed we already record them as DropGlue with a <code>None</code> as the Ty -- but we're still generating two distinct instances, even with -Zpolymorphize=on. In this trivial case it's not so bad; but for example on style-servo there's 1575 drop_in_place's that take references (and so clearly have trivial drop glue, there's probably dozens more which are still trivial, just not as noticeably so).</p>
<div class="codehilite"><pre><span></span><code><span class="vg">@vtable.0</span> <span class="p">=</span> <span class="k">private</span> <span class="k">unnamed_addr</span> <span class="k">constant</span> <span class="p">{</span> <span class="k">void</span> <span class="p">(</span><span class="k">i64</span><span class="p">*)*,</span> <span class="k">i64</span><span class="p">,</span> <span class="k">i64</span> <span class="p">}</span> <span class="p">{</span> <span class="k">void</span> <span class="p">(</span><span class="k">i64</span><span class="p">*)*</span> <span class="vg">@core</span><span class="err">::</span><span class="nl">ptr:</span><span class="err">:drop_in_place</span><span class="p">,</span> <span class="k">i64</span> <span class="m">8</span><span class="p">,</span> <span class="k">i64</span> <span class="m">8</span> <span class="p">},</span> <span class="k">align</span> <span class="m">8</span>
<span class="vg">@vtable.1</span> <span class="p">=</span> <span class="k">private</span> <span class="k">unnamed_addr</span> <span class="k">constant</span> <span class="p">{</span> <span class="k">void</span> <span class="p">(</span><span class="k">i64</span><span class="p">*)*,</span> <span class="k">i64</span><span class="p">,</span> <span class="k">i64</span> <span class="p">}</span> <span class="p">{</span> <span class="k">void</span> <span class="p">(</span><span class="k">i64</span><span class="p">*)*</span> <span class="vg">@core</span><span class="err">::</span><span class="nl">ptr:</span><span class="err">:drop_in_place</span><span class="p">,</span> <span class="k">i64</span> <span class="m">8</span><span class="p">,</span> <span class="k">i64</span> <span class="m">8</span> <span class="p">},</span> <span class="k">align</span> <span class="m">8</span>

<span class="c">; core::ptr::drop_in_place</span>
<span class="c">; Function Attrs: nonlazybind uwtable</span>
<span class="k">define</span> <span class="k">internal</span> <span class="k">void</span> <span class="vg">@core</span><span class="err">::</span><span class="nl">ptr:</span><span class="err">:drop_in_place</span><span class="p">(</span><span class="k">i64</span><span class="p">*</span> <span class="nv">%_1</span><span class="p">)</span> <span class="k">unnamed_addr</span> <span class="vg">#0</span> <span class="p">{</span>
<span class="nl">start:</span>
  <span class="nv nv-Anonymous">%0</span> <span class="p">=</span> <span class="k">alloca</span> <span class="p">{},</span> <span class="k">align</span> <span class="m">1</span>
  <span class="k">ret</span> <span class="k">void</span>
<span class="p">}</span>

<span class="c">; core::ptr::drop_in_place</span>
<span class="c">; Function Attrs: nonlazybind uwtable</span>
<span class="k">define</span> <span class="k">internal</span> <span class="k">void</span> <span class="vg">@core</span><span class="err">::</span><span class="nl">ptr:</span><span class="err">:drop_in_place</span><span class="p">(</span><span class="k">i64</span><span class="p">*</span> <span class="nv">%_1</span><span class="p">)</span> <span class="k">unnamed_addr</span> <span class="vg">#0</span> <span class="p">{</span>
<span class="nl">start:</span>
  <span class="nv nv-Anonymous">%0</span> <span class="p">=</span> <span class="k">alloca</span> <span class="p">{},</span> <span class="k">align</span> <span class="m">1</span>
  <span class="k">ret</span> <span class="k">void</span>
<span class="p">}</span>

<span class="c">; t::usize_</span>
<span class="c">; Function Attrs: nonlazybind uwtable</span>
<span class="k">define</span> <span class="p">{</span> <span class="p">{}*,</span> <span class="p">[</span><span class="m">3</span> <span class="k">x</span> <span class="k">i64</span><span class="p">]*</span> <span class="p">}</span> <span class="vg">@t</span><span class="err">::usize_</span><span class="p">(</span><span class="k">i64</span><span class="p">*</span> <span class="k">noalias</span> <span class="k">readonly</span> <span class="k">align</span> <span class="m">8</span> <span class="k">dereferenceable</span><span class="p">(</span><span class="m">8</span><span class="p">)</span> <span class="nv">%v</span><span class="p">)</span> <span class="k">unnamed_addr</span> <span class="vg">#0</span> <span class="p">{</span>
<span class="nl">start:</span>
  <span class="nv">%_3.0</span> <span class="p">=</span> <span class="k">bitcast</span> <span class="k">i64</span><span class="p">*</span> <span class="nv">%v</span> <span class="k">to</span> <span class="p">{}*</span>
  <span class="nv nv-Anonymous">%0</span> <span class="p">=</span> <span class="k">insertvalue</span> <span class="p">{</span> <span class="p">{}*,</span> <span class="p">[</span><span class="m">3</span> <span class="k">x</span> <span class="k">i64</span><span class="p">]*</span> <span class="p">}</span> <span class="k">undef</span><span class="p">,</span> <span class="p">{}*</span> <span class="nv">%_3.0</span><span class="p">,</span> <span class="m">0</span>
  <span class="nv nv-Anonymous">%1</span> <span class="p">=</span> <span class="k">insertvalue</span> <span class="p">{</span> <span class="p">{}*,</span> <span class="p">[</span><span class="m">3</span> <span class="k">x</span> <span class="k">i64</span><span class="p">]*</span> <span class="p">}</span> <span class="nv nv-Anonymous">%0</span><span class="p">,</span> <span class="p">[</span><span class="m">3</span> <span class="k">x</span> <span class="k">i64</span><span class="p">]*</span> <span class="k">bitcast</span> <span class="p">({</span> <span class="k">void</span> <span class="p">(</span><span class="k">i64</span><span class="p">*)*,</span> <span class="k">i64</span><span class="p">,</span> <span class="k">i64</span> <span class="p">}*</span> <span class="vg">@vtable.0</span> <span class="k">to</span> <span class="p">[</span><span class="m">3</span> <span class="k">x</span> <span class="k">i64</span><span class="p">]*),</span> <span class="m">1</span>
  <span class="k">ret</span> <span class="p">{</span> <span class="p">{}*,</span> <span class="p">[</span><span class="m">3</span> <span class="k">x</span> <span class="k">i64</span><span class="p">]*</span> <span class="p">}</span> <span class="nv nv-Anonymous">%1</span>
<span class="p">}</span>

<span class="c">; t::u64_</span>
<span class="c">; Function Attrs: nonlazybind uwtable</span>
<span class="k">define</span> <span class="p">{</span> <span class="p">{}*,</span> <span class="p">[</span><span class="m">3</span> <span class="k">x</span> <span class="k">i64</span><span class="p">]*</span> <span class="p">}</span> <span class="vg">@t</span><span class="err">::u</span><span class="m">64</span><span class="err">_</span><span class="p">(</span><span class="k">i64</span><span class="p">*</span> <span class="k">noalias</span> <span class="k">readonly</span> <span class="k">align</span> <span class="m">8</span> <span class="k">dereferenceable</span><span class="p">(</span><span class="m">8</span><span class="p">)</span> <span class="nv">%v</span><span class="p">)</span> <span class="k">unnamed_addr</span> <span class="vg">#0</span> <span class="p">{</span>
<span class="nl">start:</span>
  <span class="nv">%_3.0</span> <span class="p">=</span> <span class="k">bitcast</span> <span class="k">i64</span><span class="p">*</span> <span class="nv">%v</span> <span class="k">to</span> <span class="p">{}*</span>
  <span class="nv nv-Anonymous">%0</span> <span class="p">=</span> <span class="k">insertvalue</span> <span class="p">{</span> <span class="p">{}*,</span> <span class="p">[</span><span class="m">3</span> <span class="k">x</span> <span class="k">i64</span><span class="p">]*</span> <span class="p">}</span> <span class="k">undef</span><span class="p">,</span> <span class="p">{}*</span> <span class="nv">%_3.0</span><span class="p">,</span> <span class="m">0</span>
  <span class="nv nv-Anonymous">%1</span> <span class="p">=</span> <span class="k">insertvalue</span> <span class="p">{</span> <span class="p">{}*,</span> <span class="p">[</span><span class="m">3</span> <span class="k">x</span> <span class="k">i64</span><span class="p">]*</span> <span class="p">}</span> <span class="nv nv-Anonymous">%0</span><span class="p">,</span> <span class="p">[</span><span class="m">3</span> <span class="k">x</span> <span class="k">i64</span><span class="p">]*</span> <span class="k">bitcast</span> <span class="p">({</span> <span class="k">void</span> <span class="p">(</span><span class="k">i64</span><span class="p">*)*,</span> <span class="k">i64</span><span class="p">,</span> <span class="k">i64</span> <span class="p">}*</span> <span class="vg">@vtable.1</span> <span class="k">to</span> <span class="p">[</span><span class="m">3</span> <span class="k">x</span> <span class="k">i64</span><span class="p">]*),</span> <span class="m">1</span>
  <span class="k">ret</span> <span class="p">{</span> <span class="p">{}*,</span> <span class="p">[</span><span class="m">3</span> <span class="k">x</span> <span class="k">i64</span><span class="p">]*</span> <span class="p">}</span> <span class="nv nv-Anonymous">%1</span>
<span class="p">}</span>
</code></pre></div>



<a name="206516187"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216091-t-compiler/wg-polymorphization/topic/drop_in_place%20polymorphization/near/206516187" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> davidtwco <a href="https://rust-lang.github.io/zulip_archive/stream/216091-t-compiler/wg-polymorphization/topic/drop_in_place.20polymorphization.html#206516187">(Aug 10 2020 at 21:32)</a>:</h4>
<p>Currently polymorphization doesn’t run on shims (<a href="https://github.com/rust-lang/rust/blob/1275cc15d6aa0597f0b3bbf8aa0750239f9da7b2/src/librustc_middle/ty/instance.rs#L476">due to this line</a>). I think the primary reason for that restriction is that it meant we hit fewer ICEs when getting things working initially - I’ll use this example when experimenting in the next few days.</p>



<a name="206605749"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216091-t-compiler/wg-polymorphization/topic/drop_in_place%20polymorphization/near/206605749" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> davidtwco <a href="https://rust-lang.github.io/zulip_archive/stream/216091-t-compiler/wg-polymorphization/topic/drop_in_place.20polymorphization.html#206605749">(Aug 11 2020 at 17:38)</a>:</h4>
<p>Looked into this briefly today (submitting <a href="https://github.com/rust-lang/rust/issues/75414">#75414</a>) - polymorphization works if it runs on shims, but it won't optimize this case w/out <a class="stream-topic" data-stream-id="216091" href="/#narrow/stream/216091-t-compiler.2Fwg-polymorphization/topic/T.20behind.20references.2Fpointers">#t-compiler/wg-polymorphization &gt; T behind references/pointers</a>.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>